﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


public class AreaFullInfoDAL
    {
        #region 开始生成tomodel提高代码的重用性

        /// 开始生成tomodel提高代码的重用性
        private AreaFullInfoModel ToModel( DataRow row )
        {
            var model = new AreaFullInfoModel();
            model.AreaId = (Int32) SqlHelper.FromDbValue(row["AreaId"]);
            model.AreaName = (String) SqlHelper.FromDbValue(row["AreaName"]);
            model.AreaPid = (Int32) SqlHelper.FromDbValue(row["AreaPid"]);
            return model;
        } //tomodel的结束。

        #endregion

        #region listAll开始

        //查询所有AreaFullInfo的数据列表
        public IEnumerable <AreaFullInfoModel> ListAll()
        {
            var list = new List <AreaFullInfoModel>();
            DataTable table = SqlHelper.ExecuteDataTable("Select  AreaId,AreaName,AreaPid FROM  AreaFullInfo ");
            foreach (DataRow row in table.Rows)
                {
                    AreaFullInfoModel model = ToModel(row);
                    list.Add(model);
                }
            return list;
        } //listAllAreaFullInfo结束

        #endregion






        #region  根据ID来查询AreaFullInfo的信息

        public AreaFullInfoModel[] GetByAreaFullInfoId(int areaPid)
        {
            DataTable table = SqlHelper.ExecuteDataTable
                    (
                            " SELECT AreaId,AreaName,AreaPid FROM  AreaFullInfo  WHERE  AreaPid = @AreaPid", new SqlParameter("@AreaPid",areaPid));
         AreaFullInfoModel [] models=new AreaFullInfoModel[table.Rows.Count];

            for (int i = 0; i < table.Rows.Count; i++)
                {
               models[i]=     ToModel(table.Rows[i]);

                }
            return models;

        }

        #endregion

        #region  根据ID修改AreaFullInfo的信息

        public void UpdateAreaFullInfo( AreaFullInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "  UPDATE  AreaFullInfo SET  AreaId = @AreaId,AreaName = @AreaName,AreaPid = @AreaPid  WHERE  AreaId = @AreaId",
                            new SqlParameter("@AreaId", model.AreaId),
                            new SqlParameter("@AreaName", model.AreaName),
                            new SqlParameter("@AreaPid", model.AreaPid));
        }

        #endregion

        #region 添加AreaFullInfo信息

        // 添加一条AreaFullInfo 的信息
        public void InsertAreaFullInfo( AreaFullInfoModel model )
        {
            SqlHelper.ExecuteNonQuery
                    (
                            "INSERT INTO AreaFullInfo  (AreaId,AreaName,AreaPid  ) values (@AreaId,@AreaName,@AreaPid)",
                            new SqlParameter("@AreaId", model.AreaId),
                            new SqlParameter("@AreaName", model.AreaName),
                            new SqlParameter("@AreaPid", model.AreaPid));
        }

        #endregion


        #region 查询所有省市列表ListProvAll

        //查询所有AreaFullInfo的数据列表
        public IEnumerable<AreaFullInfoModel> ListProvAll()
        {
            var list = new List<AreaFullInfoModel>();
            DataTable table = SqlHelper.ExecuteDataTable("Select  AreaId,AreaName,AreaPid FROM  AreaFullInfo WHERE AreaPid=0 ");
            foreach (DataRow row in table.Rows)
            {
                AreaFullInfoModel model = ToModel(row);
                list.Add(model);
            }
            return list;
        } //listAllAreaFullInfo结束

        #endregion
    }

//当前AreaFullInfoDAL类结束